/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.azon.left.hand;

/**
 *
 * @author Mahdan Ahmad F A
 */
public class ShapeGuesser extends Guesser{

    @Override
    public String doGueess(String chain_code) {
        
        String shape = "";
        
        String  code        = getChainCode(chain_code);
        int[]   frequency   = getFrequency(chain_code);
        String  sequence    = getSequence(chain_code);
        
        if (maybeSquere(code, sequence, frequency)) {
            shape = "persegi";
        } else if (maybeTriangle(code, sequence, frequency)) {
            shape = "segitiga";
        } else if (maybeDiamond(code, sequence, frequency)) {
            shape = "wajik";
        } else if (maybeCircle(code, sequence, frequency)) {
            shape = "lingkaran";
        } else if (maybeHexagon(code, sequence, frequency)) {
            shape = "segi enam";
        }
        
        return shape;
    }
    
    private boolean maybeSquere(String code, String sequence, int[] frequence) {
        
        return sequence.length() == 4;
    }
    
    private boolean maybeTriangle(String code, String sequence, int[] frequence) {
        int garisMiring = cekGarisMiring(sequence);
        if (sequence.length() == 3) {
            return true;
        } else {
            if (garisMiring == 2) {
                return true;
            }
        }
        
        return false;
    }
    
    private boolean maybeDiamond(String code, String sequence, int[] frequence) {
        int garisMiring = cekGarisMiring(sequence);
        int garisLurus  = cekGarisLurus(code);
        
        if (garisMiring == 4 && !(maybeCircle(code, sequence, frequence))) {
            return garisLurus == 0;
        }
        
        return false;
    }
    
    private boolean maybeCircle(String code, String sequence, int[] frequence) {
        String anotherSequence = "";
        
        for(int i = 0; i < sequence.length(); i++) {
            if (!anotherSequence.contains("" + sequence.charAt(i))) {
                anotherSequence += sequence.charAt(i);
            } else {
                
            }
        }
        
        if (anotherSequence.equals("69874123") || anotherSequence.equals("98741236")) {
            return !maybeHexagon(code, sequence, frequence);
        }
        
        return false;
    }
    
    private boolean maybeHexagon(String code, String sequence, int[] frequence) {
        int garisLurus  = cekGarisLurus(code);
        int garisMiring = cekGarisMiring(sequence);
        
        return (garisLurus == 2) && (garisMiring == 4);
    }
    
}
